﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using BaseTerminalProtocol.Authorizations;
using Helpers;

namespace BaseTerminalProtocol.TerminalActions.Providers
{
    public class AddOfflinePayment
    {
        /// <summary>
        /// Requests the add offline payment.
        /// </summary>
        /// <param name="authorization">The pv author.</param>
        /// <param name="paymentID">The payment ID.</param>
        /// <param name="serviceID">The service ID.</param>
        /// <param name="account">The account.</param>
        /// <param name="pvAmount">The pv amount.</param>
        /// <param name="receiptID">The receipt ID.</param>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        private static XmlDocument RequestAddOfflinePayment(Authorization authorization, string paymentID, string serviceID, string account, double pvAmount, string receiptID, DateTime date)
        {
            // Init XML Document            
            XmlDocument doc = XMLGate.Request_INIT(authorization);
            XmlElement _xml_Root = doc.DocumentElement;
            //provider
            XmlElement _xml_provider = XMLHelper.newElement(doc, "providers");
            XmlElement _xml_addOfflinePayment = XMLHelper.newElement(doc, "addOfflinePayment");
            //addOfflinePayment
            //payment 
            XmlElement _xml_payment = XMLHelper.newElement(doc, "payment");
            _xml_payment.Attributes.Append(XMLHelper.newAttribute(doc, "id", paymentID));
            //from
            XmlElement _xml_from = XMLHelper.newElement(doc, "from");
            _xml_from.Attributes.Append(XMLHelper.newAttribute(doc, "currency", "643"));
            _xml_from.Attributes.Append(XMLHelper.newAttribute(doc, "amount", pvAmount.ToString()));
            //to
            XmlElement _xml_to = XMLHelper.newElement(doc, "to");
            _xml_to.Attributes.Append(XMLHelper.newAttribute(doc, "currency", "643"));
            _xml_to.Attributes.Append(XMLHelper.newAttribute(doc, "service", serviceID));
            _xml_to.Attributes.Append(XMLHelper.newAttribute(doc, "amount", pvAmount.ToString()));
            _xml_to.Attributes.Append(XMLHelper.newAttribute(doc, "account", account));
            //receipt
            XmlElement _xml_receipt = XMLHelper.newElement(doc, "receipt");
            _xml_receipt.Attributes.Append(XMLHelper.newAttribute(doc, "id", receiptID));
            _xml_receipt.Attributes.Append(XMLHelper.newAttribute(doc, "date", date.ToString("yyyy-MM-ddThh:mm:ss")));
            //add to payment
            _xml_payment.AppendChild(_xml_from);
            _xml_payment.AppendChild(_xml_to);
            _xml_payment.AppendChild(_xml_receipt);
            //add to addOfflinePayment
            _xml_addOfflinePayment.AppendChild(_xml_payment);
            //add to provider
            _xml_provider.AppendChild(_xml_addOfflinePayment);
            //add to doc
            _xml_Root.AppendChild(_xml_provider);
            return doc;
        }

        /// <summary>
        /// Responses the add offline payment.
        /// </summary>
        /// <param name="pvPaymentID">The pv payment ID.</param>
        /// <param name="pvServiceID">The pv service ID.</param>
        /// <param name="pvAccount">The pv account.</param>
        /// <param name="pvAmount">The pv amount.</param>
        /// <param name="pvReceiptID">The pv receipt ID.</param>
        /// <param name="pvdate">The pvdate.</param>
        /// <returns></returns>
        public static XMLGate.GateResponse ResponseAddOfflinePayment(string pvPaymentID, string pvServiceID, string pvAccount, double pvAmount, string pvReceiptID, DateTime pvdate)
        {
            try
            {
                var authorization = new Authorization();
                string request = "";
                var requestAddofflinePayment = RequestAddOfflinePayment(authorization, pvPaymentID, pvServiceID, pvAccount, pvAmount, pvReceiptID, pvdate);
                var requestSend = XMLGate.Request_SEND(requestAddofflinePayment, authorization.ServerUrl, out request);
                var response = new XMLGate.GateResponse(requestSend);
                return response;
            }
            catch (Exception ex)
            {
                Tools.Logger("AddOfflinePayment : " + ex.Message, "error", "ResponseAddOfflinePayment");
            }
            return null;
        }
    }
}
